package com.vidmt.api.modules.sys.api;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.vidmt.api.common.exceptions.BadParamException;
import com.vidmt.api.common.pojo.ApiResultServer;
import com.vidmt.api.common.web.ApiController;
import com.vidmt.api.modules.sys.entity.PhoneCity;
import com.vidmt.api.modules.sys.service.PhoneCityService;

@Controller
@RequestMapping("/api/1/utils")
public class UtilController extends ApiController {
	private static final String PHONE_PATTERN = "^1[3578]\\d{9}$";
	
	@Autowired
	private PhoneCityService phonecityService;

	@ResponseBody
	@RequestMapping("/getphonecity.*")
	public ApiResultServer getPhoneCity(String phone) throws BadParamException {
		if (phone == null || !phone.matches(PHONE_PATTERN)) {
			throw new BadParamException("手机号格式错误:" + phone);
		}
		PhoneCity pc = phonecityService.getByPhone(phone);
		if (pc == null) {
			return new ApiResultServer();
		}
		return new ApiResultServer(pc);
	}
}
